package com.halflife;

import com.halflife.shiro.filter.AuthenticationFilter;
import com.halflife.shiro.realm.MyAuthorizingRealm;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Created by zhaoyf on 2018/2/24.
 */
@Configuration
public class ShiroConfig {
    @Bean
    public ShiroFilterFactoryBean shiroFilter(SecurityManager securityManager) {
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(securityManager);

        Map<String, String> filterChainDefinitionMap = new LinkedHashMap<>();
        filterChainDefinitionMap.put("/static/**", "anon");
        filterChainDefinitionMap.put("/**/notify.do", "anon");
        filterChainDefinitionMap.put("/**/notify_.do", "anon");
        filterChainDefinitionMap.put("/**/*.css", "anon");
        filterChainDefinitionMap.put("/**/*.jpg", "anon");
        filterChainDefinitionMap.put("/**/*.png", "anon");
        filterChainDefinitionMap.put("/**/test/**", "anon");
        filterChainDefinitionMap.put("/", "anon");
        filterChainDefinitionMap.put("/xx", "anon");
        filterChainDefinitionMap.put("/yy", "anon");
        filterChainDefinitionMap.put("/**/*.js", "anon");
        filterChainDefinitionMap.put("/mc_login/**", "anon");
        filterChainDefinitionMap.put("/rider_login/**", "anon");
        filterChainDefinitionMap.put("/rider/**", "anon");
        filterChainDefinitionMap.put("/admin/login", "anon");
        filterChainDefinitionMap.put("/front/**", "anon");
        filterChainDefinitionMap.put("/api/**", "anon");
        filterChainDefinitionMap.put("/svg/**", "anon");
        filterChainDefinitionMap.put("/**", "authc");
        shiroFilterFactoryBean.setLoginUrl("/login");
        shiroFilterFactoryBean.setSuccessUrl("/login");


        shiroFilterFactoryBean.setUnauthorizedUrl("/403");
        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);

        Map<String, Filter> filters = new HashMap<>();
        Map<String, String> param = new HashMap<>();
        param.put("prefix_admin", "prefix_admin");
        param.put("prefix_rider", "prefix_rider");
        param.put("prefix_merchant", "prefix_merchant");
        filters.put("authc",new AuthenticationFilter(param));
        shiroFilterFactoryBean.setFilters(filters);

        return shiroFilterFactoryBean;
    }
    @Bean
    public SecurityManager securityManager(MyAuthorizingRealm myAuthorizingRealm){
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(myAuthorizingRealm);
        return securityManager;
    }
    @Bean
    public MyAuthorizingRealm myAuthorizingRealm(){
        return new MyAuthorizingRealm();

    }

//    @Bean
//    public AuthenticationFilter authenticationFilter(){
//        return new AuthenticationFilter();
//    }
}
